﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NSAP.cs" company="Rolosoft Ltd">
//   © Rolosoft Ltd
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

#region License

// Copyright 2013 Rolosoft Ltd
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Rsft.Net.Dns.Entities.Records
{
    #region Usings

    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Globalization;
    using System.Text;

    using Rsft.Net.Dns.Interfaces;

    #endregion

    /// <summary>
    ///     The NSAP record.
    /// </summary>
    public sealed class NSAP : Record
    {
        #region Fields

        /// <summary>
        ///     The NSAP address.
        /// </summary>
        private readonly byte[] nsapAddress;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="NSAP"/> class.
        /// </summary>
        /// <param name="recordReader">
        /// The record reader.
        /// </param>
        public NSAP(IRecordReader recordReader)
            : base(recordReader)
        {
            Contract.Requires(recordReader != null);

            this.Length = recordReader.ReadUInt16();
            this.nsapAddress = recordReader.ReadBytes(this.Length);
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="NSAP" /> class.
        /// </summary>
        public NSAP()
        {
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets Length.
        /// </summary>
        public ushort Length { get; private set; }

        /// <summary>
        ///     Gets Nsap address.
        /// </summary>
        public IEnumerable<byte> NsapAddress
        {
            get
            {
                return this.nsapAddress;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///     Toes the GOSIP v2.
        /// </summary>
        /// <returns>
        ///     The to gosip v 2.
        /// </returns>
        public string ToGOSIPV2()
        {
            return string.Format(
                CultureInfo.InvariantCulture, 
                "{0:X}.{1:X}.{2:X}.{3:X}.{4:X}.{5:X}.{6:X}{7:X}.{8:X}", 
                this.nsapAddress[0], 
                // AFI
                this.nsapAddress[1] << 8 | this.nsapAddress[2], 
                // IDI
                this.nsapAddress[3], 
                // DFI
                this.nsapAddress[4] << 16 | this.nsapAddress[5] << 8 | this.nsapAddress[6], 
                // AA
                this.nsapAddress[7] << 8 | this.nsapAddress[8], 
                // Rsvd
                this.nsapAddress[9] << 8 | this.nsapAddress[10], 
                // RD
                this.nsapAddress[11] << 8 | this.nsapAddress[12], 
                // Area
                this.nsapAddress[13] << 16 | this.nsapAddress[14] << 8 | this.nsapAddress[15], 
                // ID-High
                this.nsapAddress[16] << 16 | this.nsapAddress[17] << 8 | this.nsapAddress[18], 
                // ID-Low
                this.nsapAddress[19]);
        }

        /// <summary>
        ///     Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        ///     A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.AppendFormat("{0} ", this.Length);
            foreach (byte t in this.NsapAddress)
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "{0:X00}", t);
            }

            return sb.ToString();
        }

        #endregion
    }
}